<?php
/**
 * @Notes:【】
 * @Date: 2022-06-21 22:28
 */

namespace Yj\Daos;

use Hyperf\Database\Model\Model;

class Search
{

    /**
     * The model being queried.
     *
     * @var \Hyperf\Database\Model\Model
     */
    protected $model;

    protected $dao;

    protected $wheres = [];

    protected $query = null;


    public function search($params = null)
    {
        if (is_array($params)) {
            $params = _Collect($params);
        }
        $params->transform(function () {

        });

        return $this;
    }


    public function __call($method, $parameters)
    {
        if (isset($this->dao) && method_exists($this->dao, $scope = 'search' . ucfirst($method))) {
            return $this->callSearch([$this->dao, $scope], $parameters);
        }

        return call([$this->getQuery(), $method], $parameters);
    }

    private function callSearch(callable $scope, $parameters)
    {
//        array_unshift($parameters, $this->model);
        $result = $scope(...array_values($parameters));

        $this->getQuery()->addNestedWhereQuery($result->getQuery());

        return $this;
    }

    /**
     * Set a model instance for the model being queried.
     *
     * @param \Hyperf\Database\Model\Model $model
     * @return $this
     */
    public function setModel(Model $model)
    {
        $this->model = $model;

        return $this;
    }

    public function getQuery()
    {
        return $this->query;
    }


    public function getNewQuery()
    {
        $this->query = $this->dao->model->newQuery();
        return $this->query;
    }


    public function setDao(BaseDaos $dao)
    {
        $this->dao = $dao;
        $this->getNewQuery();
        return $this;
    }


}